package com.laoye.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laoye.Exception.exception.BaseException;
import com.laoye.annotation.Log;
import com.laoye.bean.Product;
import com.laoye.dto.*;
import com.laoye.mapper.ProductMapper;
import com.laoye.service.ProductService;
import com.laoye.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @CacheEvict(value = {"allProducts", "simpleProduct"}, allEntries = true)
    @Override
    @Log("添加商品")
    public void addProduct(Product product) {
        if (product == null){
            throw new BaseException(401,"商品不能为空");
        }
        productMapper.addProduct(product);
    }
    @CacheEvict(value = {"allProducts", "simpleProduct","productStore"}, allEntries = true)
    @Override
    @Log("修改商品")
    public void updateProduct(Product product) {
        if (product == null|| product.getId() == null){
            throw new BaseException(401,"要修改的商品不存在");
        }
        productMapper.updateProduct( product);
    }
    @Cacheable(
            value = "allProducts",
            // 缓存键格式：prodList_商品名_分类_状态_页码_页大小（处理null参数）
            key = "'prodList_' + (#prod_name ?: 'null') + '_' + (#category ?: 'null') + '_' + (#status ?: 'null') + '_' + #pageNum + '_' + #pageSize",
            unless = "#result == null || #result.getTotal() == 0"
    )
    @Log("获取所有商品")
    @Override //获取所有商品,管理员端的
    public Page<Product> getAllProducts(String prod_name,
                                        String category,
                                        Byte status,
                                        Integer pageNum,
                                        Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productMapper.getAllProducts(prod_name, category, status);
        PageInfo<Product> pageInfo = null;
        if (products != null) {
            pageInfo = new PageInfo<>(products);
        }

        if (pageInfo != null) {
            return Page.<Product>builder()
                    .list(products)
                    .total(pageInfo.getTotal())
                    .totalPages(pageInfo.getPages())
                    .pageNum(pageInfo.getPageNum())
                    .pageSize(pageInfo.getPageSize())
                    .build();
        }


        return null;
    }

    @CacheEvict(value = {"allProducts", "simpleProduct"}, allEntries = true)
    @Override //修改商品状态上下架
    @Log("修改商品状态")
    public void updateStatus(StatusDTO statusDTO) {
        if (statusDTO == null || statusDTO.getId() == null){
            throw new BaseException(401,"要修改状态的商品不存在");
        }
        productMapper.updateStatus(statusDTO);
    }

    @Cacheable(
            value = "simpleProduct",
            key = "'allSimpleProducts'",
            unless = "#result == null || #result.isEmpty()"
    )
    @Override //获取简单商品信息，展示在前端界面
    @Log(value="获取简单商品信息",printParam = false)
    public List<ProdSimpleDTO> getSimpleProduct() {
        return productMapper.getSimpleProduct();
    }

    @Cacheable(
            value = "productDetail",
            key = "'prodDetail_' + #id",
            unless = "#result == null"
    )
    @Override
    @Log("获取商品详情")
    public ProductInfoDTO getProductById(Integer id) {
        if (id == null){
            throw new BaseException(401,"商品不存在");
        }
         return productMapper.getProductById(id);   //点击商品进入详情页面
    }
    @Cacheable(
            value = "productStore",
            key = "'prodStore_' + #id",
            unless = "#result == null"
    )
    @Override
    @Log("获取商品库存")
    public Integer getStore(Integer id) {
        if (id == null){
            throw new BaseException(401,"商品不存在");
        }
        return productMapper.getStore(id);  //获取商品库存
    }

    @Cacheable(
            value = "simpleProduct",
            key = "'simpleByCategory_' + (#category ?: 'null') + '_' + (#prod_name ?: 'null')",
            unless = "#result == null || #result.isEmpty()"
    )
    @Override
    public List<ProdSimpleDTO> getSimpleProductByCategory(String  category, String prod_name) {
        return productMapper.getSimpleProductByCategory(category, prod_name);
    }

    @CacheEvict(
            value = {"simpleProduct", "productStore", "allProducts"},
            allEntries = true
    )
    @Override
    @Log("修改商品库存")
    public void updateStore(ProductStoreDTO productStoreDTO) {
        if (productStoreDTO == null || productStoreDTO.getId() == null){
            throw new BaseException(401,"商品不存在");
        }
        productMapper.updateStore(productStoreDTO);
    }
}
